//
//  SXVisitorView.swift
//  sina
//
//  Created by shengxinwei on 16/5/20.
//  Copyright © 2016年 shengxinwei. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol SXVisitorViewDelegate: NSObjectProtocol {
    
    
    optional func requiredLogin();
    func requiredResigter();

}

class SXVisitorView: UIView {
    
    //设置代理属性
    weak var visitorDelegate: SXVisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //设置约束
        setUpUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func  updateUI(image: UIImage,labString: String,firstBool: Bool = false) {
        homeImg.image = image
        messageLabel.text = labString
        if firstBool {
           //开启动画
            animation()
        }else {
            //隐藏圆环
            circleImg.hidden = true
        }
    }
    
    //创建设置动画
    func animation() {
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画时间
        ani.duration = 10;
        ani.toValue = 2 * M_PI
        //动画重复次数
        ani.repeatCount = MAXFLOAT
        //是否移除动画
        ani.removedOnCompletion = false;
        //添加动画
        circleImg.layer.addAnimation(ani, forKey: nil)
        
    }
    
    
    func setUpUI() {
        //添加子控件
        addSubview(circleImg)
        addSubview(shadowIcon)
        addSubview(homeImg)
        addSubview(loginBtn)
        addSubview(registerBtn)
        addSubview(messageLabel)
        

        //设置约束
        homeImg.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-80)
        }
        circleImg.snp_makeConstraints { (make) in
            make.center.equalTo(homeImg)
        }
        //遮罩图片
        shadowIcon.snp_makeConstraints { (make) in
            make.center.equalTo(homeImg)
        }
        
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.top.equalTo(circleImg.snp_bottom).offset(16)
            make.width.equalTo(230)
           make.height.equalTo(40)
        }
        loginBtn.snp_makeConstraints { (make) in
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.left.equalTo(self.snp_centerX).offset(-100)
            make.width.equalTo(80)
        }
        registerBtn.snp_makeConstraints { (make) in
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.left.equalTo(self.snp_centerX).offset(16)
            make.width.equalTo(80)
        }
        
        loginBtn.addTarget(self, action: #selector(loginBtnClick), forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: #selector(registerBtnClick), forControlEvents: .TouchUpInside)
        
    }
    
    //登陆
    @objc private func loginBtnClick() {
        visitorDelegate?.requiredLogin?()
    }
    //注册
    @objc private func registerBtnClick() {
        visitorDelegate?.requiredResigter()
    }

    
    //MARK: - 懒加载
    //圆环
    private lazy var circleImg: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //登陆按钮
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        self.stretchImage(btn)
//        btn.setBackgroundImage(UIImage.init(named: "common_button_white_disable"), forState: .Normal)
        return btn
    }()

    //注册按钮
    private lazy var registerBtn: UIButton = {
        let btn = UIButton()
        btn.setTitle("注册", forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        self.stretchImage(btn)
        
        return btn
    }()
    

    //中间home图片
    private lazy var homeImg: UIImageView = UIImageView()
    
    //提示文字
    private lazy var messageLabel: UILabel = {
       let lab = UILabel()
//        lab.text = "关注一些人，回这里看看有什么惊喜"
        lab.textAlignment = .Center
        lab.textColor = UIColor.darkGrayColor()
        lab.font = UIFont.systemFontOfSize(14.0)
        lab.numberOfLines = 0
        return lab
    }()

    //遮罩阴影
    private lazy var shadowIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    //剪裁图片
    func stretchImage(btn: UIButton) {
        let image: UIImage = UIImage(named: "common_button_white_disable")!
        let capW = Int(image.size.width * 0.5)
        let capH = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(capW, topCapHeight: capH), forState: .Normal)
    }
    

}
